package com.situ.company.employee.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class EmployeeDaoImpl implements IEmployeeDao{
	private static String table = "employee";
	private static String cols ="code,name,pass,code_dept";

	@Override
	public Integer insert(EmployeeModel model) {
		String sql = String.format("insert into %s(%s) values(?,?,?,?)", table, cols);
		return JDBCUtil.update(sql, model.getCode(),model.getName(),model.getPass(), model.getCodeDept());
	}

	@Override
	public Integer deleteByCode(EmployeeModel model) {
		String sql = String.format("delete from %s where code = ?", table);
		return JDBCUtil.update(sql, model.getCode());
	}

	@Override
	public Integer delete(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("delete from ").append(table);
		List<Object> values = appendwhere(sql, model);
		return JDBCUtil.update(sql.toString(), values);
	}

	@Override
	public Integer updateAll(EmployeeModel model) {
		String sql = String.format("update %s set name=?, code_dept=? where code=?", table);
		return JDBCUtil.update(sql, model.getName(), model.getCodeDept(), model.getCode());
	}

	@Override
	public Integer updateActive(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("update ").append(table).append(" set id=id ");
		List<Object> values = appendSet(sql, model);
		return JDBCUtil.update(sql.toString(), values);
	}


	@Override
	public List<EmployeeModel> selectList(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("select id,").append(cols).append(" from ").append(table);
		List<Object> values = appendwhere(sql, model);
		Map<String, String> fields = new HashMap<>();
		fields.put("id", "id");
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("pass", "pass");
		fields.put("codeDept", "code_dept");
		return JDBCUtil.queryList(sql.toString(), values, EmployeeModel.class, fields);
	}

	@Override
	public EmployeeModel selectModel(EmployeeModel model) {
//		StringBuffer sql = new StringBuffer("select id, ");
//		sql.append(cols).append(" from ").append(" where code = ? ");
		String sql = String.format("select id, %s from %s where code = ?", cols, table);
		List<Object> values = Arrays.asList(model.getCode());
		Map<String, String> fields = new HashMap<>();
		fields.put("id", "id");
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("pass", "pass");
		fields.put("codeDept", "code_dept");
//		fields.put("image", "image");
		return JDBCUtil.queryModel(sql, values, EmployeeModel.class, fields);
	}

	private List<Object> appendwhere(StringBuffer sql, EmployeeModel model) {
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<>();
		String code = model.getCode();
		if (!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ? ");
			values.add(code);
		}
		String name = model.getName();
		if (!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ? ");
			values.add(name);
		}
		String pass = model.getPass();
		if (!FmtEmpty.isEmpty(pass)) {
			sql.append(" and tel like ? ");
			values.add(pass);
		}
		String codeDept = model.getCodeDept();
		if (!FmtEmpty.isEmpty(codeDept)) {
			sql.append(" and code_dept like ? ");
			values.add(pass);
		}
		String image = model.getImage();
		if (!FmtEmpty.isEmpty(image)) {
			sql.append(" and image like ? ");
			values.add(pass);
		}
		return values;

	}

	private List<Object> appendSet(StringBuffer sql, EmployeeModel model) {
		List<Object> values = new ArrayList<>();
		String name = model.getName();
		if (name != null) {
			sql.append(" ,name=? ");
			values.add(name);
		}
		String pass = model.getPass();
		if (pass != null) {
			sql.append(" ,pass=? ");
			values.add(pass);
		}
		String codeDept = model.getCodeDept();
		if (codeDept != null) {
			sql.append(" ,code_dept=? ");
			values.add(codeDept);
		}
		String image = model.getImage();
		if (image != null) {
			sql.append(" ,image=? ");
			values.add(image);
		}
		sql.append(" where code = ? ");
		values.add(model.getCode());
		return values;
	}

}
